package com.rulai.generator.plugin.terminator;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.velocity.VelocityContext;
import org.mybatis.generator.exception.XMLParserException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.rulai.generator.plugin.MybatisGeneratorUtil;
import com.rulai.generator.plugin.model.Table2Model;
import com.rulai.generator.plugin.support.VelocityUtil;

import cn.hutool.core.io.FileUtil;

/**
 * 服务层骨架
 * 
 * @since [产品/模块版本]
 * @author leiyang@qding.me
 * @version v1.0 Date:2018年5月9日 上午9:53:03
 */
public class ServiceFacadeMavenProject extends MavenSkeletonProject {

    private static final Logger LOG = LoggerFactory.getLogger(ServiceFacadeMavenProject.class);

    /**
     * tables 表模型
     */
    private List<Table2Model> tables;

    /**
     * module 模块名
     */
    private String module;

    /**
     * packageName 包名
     */
    private String packageName;

    /**
     * service_vm Service接口模板路径
     */
    private static String service_vm = "/template/Service.vm";

    /**
     * serviceMock_vm ServiceMock模板路径
     */
    private static String serviceMock_vm = "/template/ServiceMock.vm";

    public ServiceFacadeMavenProject(String projectPath, boolean repeatDel, List<Table2Model> tables,
            String packageName, String module) {
        super(projectPath, repeatDel);

        this.tables = tables;

        this.packageName = packageName;

        this.module = module;

        this.setArtifactId(module + "-facade");
    }

    /**
     * 得到项目代码绝对路径 <功能详细描述>
     * 
     * @return
     * @author leiyang@qding.me Date:2018年5月10日 下午2:36:49
     */
    private String getServicePath() {

        if (MavenSkeletonProject.osInfo.isWindows()) {
            return this.getMainJavaPath() +"/"+ packageName.replaceAll("\\.", "/") + "/" + "facade";
        } else {
            return this.getMainJavaPath() +"/"+ packageName.replaceAll("\\.", "/") + "/" + "facade";
        }

    }

    /**
     * 填充项目内容 <功能详细描述>
     * 
     * @author leiyang@qding.me Date:2018年5月9日 下午2:19:13
     * @throws XMLParserException
     * @throws IOException
     */
    @Override
    protected void fillingContent() throws Exception {

        LOG.info("========== 开始生成ServiceFacade ==========");

        if (osInfo.isWindows()) {
            service_vm = MybatisGeneratorUtil.class.getResource(service_vm).getPath().replaceFirst("/", "");
            serviceMock_vm = MybatisGeneratorUtil.class.getResource(serviceMock_vm).getPath().replaceFirst("/", "");
        } else {
            service_vm = MybatisGeneratorUtil.class.getResource(service_vm).getPath();
            serviceMock_vm = MybatisGeneratorUtil.class.getResource(serviceMock_vm).getPath();
        }
        
        String ctime = new SimpleDateFormat("yyyy/M/d").format(new Date());
        String servicePath = getServicePath();
        if(!FileUtil.exist(servicePath)){
            //创建dir
            FileUtil.mkdir(servicePath);
        }
        for (int i = 0; i < tables.size(); i++) {
            String model = tables.get(i).getModelName();
//            String service = servicePath + "/" + model + "Service.java";
//            String serviceMock = servicePath + "/" + model + "ServiceMock.java";
            // 生成service
             File serviceFile= FileUtil.file(servicePath, model + "Service.java");
            if (!serviceFile.exists()) {
                VelocityContext context = new VelocityContext();
                context.put("package_name", packageName);
                context.put("model", model);
                context.put("ctime", ctime);
                VelocityUtil.generate(service_vm, serviceFile.getAbsolutePath(), context);
                System.out.println(serviceFile.getAbsolutePath());
            }
            // 生成serviceMock
            File serviceMockFile = FileUtil.file(servicePath, model + "ServiceMock.java");
            if (!serviceMockFile.exists()) {
                VelocityContext context = new VelocityContext();
                context.put("package_name", packageName);
                context.put("model", model);
                context.put("ctime", ctime);
                VelocityUtil.generate(serviceMock_vm, serviceMockFile.getAbsolutePath(), context);
                System.out.println(serviceMockFile.getAbsolutePath());
            }

        }

        LOG.info("========== 结束生成ServiceFacade ==========");

    }

}
